package com.tsd.core.utils;

import com.tsd.core.config.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * ffMpeg工具类
 */
public class FfMpegUtil {
    public static final Logger LOGGER = LoggerFactory.getLogger(FfMpegUtil.class);
    //"D:\\temp\\ffmpeg-20200601-dd76226-win64-static\\bin\\ffmpeg.exe";
    static String exec1 = null;

    public static void main(String[] args) {
        String file = "D:\\uploads\\default\\2020\\6\\3\\2020-06-03_10.17.070.2997035765199857.quicktime";
        try {
//            convertFile2mp4(file);
            FfMpegUtil ffMpegUtil = new FfMpegUtil();
            ffMpegUtil.convertFile2mp4(file);
        } catch (Exception e) {
            LOGGER.error(ExceptionUtil.getStackTraceFromException(e));
        }
    }

    /**
     * 调用ffmpeg转换文件到mp4格式
     *
     * @param srcFilePath
     * @throws IOException
     */
    public void convertFile2mp4(String srcFilePath) {
        Runtime rt = Runtime.getRuntime();
        int r = -1;
//        Thread stoppingThread;
        Thread errorStream;
        Thread inputStream;

        Process p = null;
        try {
            if (HlpUtils.isEmpty(exec1)) {
                exec1 = Config.getParameterValue("ffmpegExe");
            }
            String cmd = exec1 + " -i " + srcFilePath + " " + srcFilePath + ".mp4";
            System.out.println(cmd);
            p = rt.exec(cmd);

//            StopTranscoder stopRunnable = new StopTranscoder(p);
//            stoppingThread = new Thread(stopRunnable);
//            stoppingThread.start();

            final InputStream is1 = p.getErrorStream();
            final InputStream is2 = p.getInputStream();

            //启动两个线程，一个线程负责读标准输出流，另一个负责读标准错误流
            GetStream streamErr = new GetStream(is1);
            errorStream = new Thread(streamErr);
            errorStream.start();


            GetStream streamIn = new GetStream(is2);
            inputStream = new Thread(streamIn);
            inputStream.start();
            System.out.println("waiting for");
            try {
                r = p.waitFor();
            } catch (InterruptedException e) {
                LOGGER.error(ExceptionUtil.getStackTraceFromException(e));
            }

        } catch (Exception e) {
            LOGGER.error(ExceptionUtil.getStackTraceFromException(e));
        } finally {
            System.out.println("p.waitFor() return =" + r);
            if (p != null) {
                p.destroy();
            }
        }
    }

    static class GetStream implements Runnable {

        private final InputStream is;

        public GetStream(InputStream stream) {
            is = stream;
        }

        @Override
        public void run() {
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            try {
                String line;
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }
            } catch (IOException e) {
                LOGGER.error(ExceptionUtil.getStackTraceFromException(e));
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                    LOGGER.error(ExceptionUtil.getStackTraceFromException(e));
                }
            }
        }
    }

    static class StopTranscoder implements Runnable {
        private final Process process;

        public StopTranscoder(Process proc) {
            this.process = proc;
        }

        @Override
        public void run() {
            try {
                System.out.println("kill before sleep");
                Thread.sleep(10000);
                System.out.println("kill after sleep");
                this.process.destroy();
            } catch (Exception e) {
                LOGGER.error(ExceptionUtil.getStackTraceFromException(e));
            }
        }
    }
}
